<template>
  <a-drawer title="入学考试科目添加（按方向）"  width=450 placement="right" :closable="false" @close="onClose"
    :visible="addVisibile" style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form="form">
      <a-row :gutter="48">
        <a-col :sm="{span:12}" :lg="{span:12}">
          <a-form-item label="院系">
            <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['ssyxm',{rules:[{required:true,message:'请选择院系'}]},{initialValue:postParams.ssyxm}]"
              @change='yxChange'>
              <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{i.dwh+"-"+ i.dwmc }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :sm="{span:12}" :lg="{span:12}">
          <a-form-item label="专业">
            <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="[
                  'sszym',
                  {
                    rules: [{ required: true, message: '请选择专业!' }],
                  },
                  {initialValue:postParams.sszym}
                ]"
              @change='zyChange'>
              <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh+"-"+i.zymc }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :sm="{span:12}" :lg="{span:12}">
          <a-form-item label="研究方向">
            <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['yjfxm',{rules:[{required:true,message:'请选择研究方向'}]},{initialValue:postParams.yjfxm}]"
              @change='yjfxChange'>
              <a-select-option v-for="i in selectMenu.yjfx" :key="i.id" :value="i.yjfxm">{{i.yjfxm.substring(i.yjfxm.length-2) +"-"+ i.yjfxmc }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :sm="{span:12}" :lg="{span:12}">
          <a-form-item label="导师">
            <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['dsxxmId',{initialValue:postParams.dsxxmId}]">
              <a-select-option v-for="(i,j) in selectMenu.teachers" :key="j" :value="i.ID">{{ i.XM }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :sm="{span:12}" :lg="{span:12}">
          <a-form-item label="科目类型">
            <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['kmlxm',
            {
               rules: [{ required: true, message: '请选择科目类型!' }],
            },
            {initialValue:postParams.kmlxm}]" @change="lxChange">
              <a-select-option v-for="i in selectMenu.kmlx" :key="i.code" :value="i.code">{{ i.name }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :sm="{span:12}" :lg="{span:12}">
          <a-form-item label="科目编码">
            <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['kmbm',
            {
               rules: [{ required: true, message: '请选择科目编码!' }],
            },
            {initialValue:postParams.kmbm}]">
              <a-select-option v-for="i in selectMenu.kmbm" :key="i.id" :value="i.kmbm">{{ i.kmbm }}-{{i.kmmc}}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <div class="drawer-bootom-button">
      <a-button style="margin-right: .8rem" @click="onClose">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
  // import moment from 'moment'
  import {mapState} from 'vuex'

  const formItemLayout = {
    labelCol: {
      span: 3
    },
    wrapperCol: {
      span: 18
    }
  }

  const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072']
  export default {
    props: {
      addVisibile: {
        default: false
      }
    },
    data() {
      return {
        formItemLayout,
        loading: false,
        postParams: {
          zsxn:''
        },
        selectMenu: {
          yx: [], // 院系
          zy: [], //专业
          yjfx: [], //研究方向
          teachers: [], //获取导师
          kmlx: [], //科目类型
          kmbm: [] //科目编码
        }
      }
    },
    beforeCreate() {
      this.form = this.$form.createForm(this)
    },
    computed: {
      ...mapState({
        systemInfo: state => state.app.systemInfo,
        TYPE0126: state => state.app['TYPE0126'] // 年级
      })
    },
    created() {
      this.initSelectMenu()
      selectArr.forEach(v => {
        this.$store.dispatch('app/setDictionary', v)
      })
    },
    methods: {
      initSelectMenu() {
        // 获取科目类型
        let allRequest = [
          this.$api.base.collegeAll()
        ]
        Promise.all(allRequest).then(res => {
          this.selectMenu = {
            yx: res[0].data
          }
          this.form.getFieldDecorator('ssyxm',{initialValue:res[0].data[0].dwh})
          this.getProssion(res[0].data[0].dwh)
          this.getCode(res[0].data[0].dwh)
          this.$api.base.dictionaryData('TYPE0430').then(res => {
            this.selectMenu.kmlx = res.data
          })
           // 获取当前学年
          this.$api.base.getSchoolYear().then(res=>{
            this.postParams.zsxn = res.data.masterAdmissionYear
          })
        })

      },
      reset() {
        // this.postParams = {}
      },
      yxChange(code) {
        console.log('form:', this.form)
        this.form.setFieldsValue({
          'sszym': ''
        })
        this.form.setFieldsValue({
          'yjfxm': ''
        })
        this.form.setFieldsValue({
          'dsxxmId': ''
        })

        this.$set(this.selectMenu, 'zy',[])
        this.$set(this.selectMenu, 'yjfx', [])
        this.$set(this.selectMenu, 'ss', [])

        if(code){
          this.getProssion(code)

        }

      },
      lxChange(code){
        this.form.setFieldsValue({
          'kmbm': ''
        })
        this.$set(this.selectMenu, 'kmbm', [])
        if(code){
        this.getCode(code)
        }
      },
      zyChange(code) {
        this.form.setFieldsValue({
          'yjfxm': ''
        })
        this.form.setFieldsValue({
          'dsxxmId': ''
        })
        this.$set(this.selectMenu, 'ss', [])
        this.$set(this.selectMenu, 'yjfx', [])
        if(code){
          this.getDirec(code)
        }
      },
      yjfxChange(code) {
        // 获取导师
        console.log(code)
        this.form.setFieldsValue({
          'dsxxmId': ''
        })
        this.$set(this.selectMenu, 'ss', [])
        if(code){
          this.getTeachers(code)
        }
      },
      // 获取专业
      getProssion(code) {
        // 获取专业
        this.$api.base.majorAll({
          lsdwh: code,zycc:'2'
        }).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      },
      // 获取导师
      getTeachers(code) {
        this.$api.base.getDirecTeachers({
          ssyxm: this.form.getFieldValue('ssyxm'),
          zszym: this.form.getFieldValue('sszym'),
          yjfx: code,
        }).then(res => {
          if (res.data && res.data.rows && res.data.rows.length) {
            res.data.rows.map((item, index) => {
              item.index = index
              return item
            })
            // console.log(res.data.rows)
            this.$set(this.selectMenu, 'teachers', res.data.rows)
          } else {
            this.$set(this.selectMenu, 'teachers', [])
          }
        })
      },
      // 获取科目编码
      getCode(code) {
        this.$api.base.subjectCode({
          ssyxm: this.form.getFieldValue('ssyxm'),
          code:code
        }).then(res => {
          this.$set(this.selectMenu, 'kmbm', res.data.rows)
        })
      },
      // 获取研究方向
      getDirec(code) {
        this.$api.base.searchDirectory({
          ssyxm: this.form.getFieldValue('ssyxm'),
          zsxn: this.postParams.zsxn,
          sszym: code,
          xwccm:2
        }).then(res => {
          this.$set(this.selectMenu, 'yjfx', res.data)

        })
      },
      // 关闭
      onClose() {
        this.reset()
        this.$emit('close')
        this.form.resetFields()
        this.initSelectMenu()
      },
      // 保存
      handleSubmit() {
        // this.reset()
        // this.$emit('success', this.postParams)
        this.form.validateFields((err, values) => {
          if (err) {
            this.$message.error("请选择必填项")
          } else {
            var kmmc;
            this.selectMenu.kmbm.map((item, index) => {
              if (item.kmbm == this.form.getFieldValue('kmbm')) {
                kmmc = item.kmmc
              }
            })
            var params = Object.assign({
              kmmc: kmmc
            }, this.form.getFieldsValue())
            this.$api.recruitstat.entryResultManageAdd(params).then(res => {
              // this.reset()
              this.$emit('success')
              this.form.resetFields()
              this.initSelectMenu()
            }).finally(() => {
              this.loadingBtn = false
            })
          }
        })
      }
    }
  }
</script>
<style type="text/css" scoped="">
  .ant-col-lg-12{height: 111px;}
</style>
